using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Xml;
using Newtonsoft.Json.Linq;

namespace BUS
{
	/// <summary> 
	///Author: nnamthach@gmail.com 
	/// <summary>
    public class TheoDoiLCDControl : BUSControl
    {
		#region Local Variable
        private TheoDoiLCDDataAccess _objDAO;
		#endregion Local Variable

	    public TheoDoiLCDControl(string type, string connectString, int timeout = 0)
            : base(type, connectString, timeout) { _objDAO = new TheoDoiLCDDataAccess(Connnection); }
			
        public TheoDoiLCDControl(BUSControl common)
            : base(common) { _objDAO = new TheoDoiLCDDataAccess(common.Connnection); }
	
		#region Method
		public DataTable GetShortData(Int64 IdSX, List<DTO.FilterParameter> filters, ref string sErr) {
            return _objDAO.GetShortData(IdSX, filters, ref sErr);
        }
       		
        public DataTable GetByFilterToDataTable(Int64 IdSX, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0) {
            return _objDAO.GetByFilterToDataTable(IdSX, filters, ref logMsg, indexPage, itemPerPage);
        }
		
		public int GetCountRecord(Int64 IdSX, List<DTO.FilterParameter> filters, ref string sErr) {
            return _objDAO.GetCountRecord(IdSX, filters, ref sErr);
        }
		
		public TheoDoiLCDInfo[] GetByFilter(Int64 IdSX, List<DTO.FilterParameter> filters, ref string logMsg, int indexPage = 0, int itemPerPage = 0) {
            TheoDoiLCDInfo[] res = _objDAO.GetByFilter(IdSX, filters, ref logMsg, indexPage, itemPerPage);
			//get details
			
			return res;
        }
		
        public int Add(TheoDoiLCDInfo obj, ref string sErr) {
            _objDAO.Add(obj, ref sErr);
			if(sErr == "") {
				//add details
			}
			return 1;
        }
		
        public string Update(TheoDoiLCDInfo obj) {
            string sErr = _objDAO.Update(obj);
			// delete details
			// add details
			return sErr;
        }
		
        public string Delete(Int64 IdSX) {
            string sErr = "";
			// delete details
			
			if(sErr == "") sErr = _objDAO.Delete(IdSX);
			return sErr;
        }  
        public bool IsExist(Int64 IdSX, ref string sErr) {
            return _objDAO.IsExist(IdSX, ref sErr);
        } 

        public string InsertUpdate(TheoDoiLCDInfo obj)
        {
            string sErr = "";
            if (IsExist(obj.IdSX, ref sErr)) { sErr = Update(obj); }
            else { 
				if(sErr =="") Add(obj, ref sErr); 
			}
            return sErr;
        }		
      
		public string TransferIn(DataRow row) {
            TheoDoiLCDInfo inf = new TheoDoiLCDInfo(row);
            return InsertUpdate(inf);
        }

        public override string ToXmlString(DTOInfo[] lInf, ref string sErr) {
            string result = "";
            try {
                if (lInf != null && lInf.Length > 0) {
                    XmlDocument doc = new XmlDocument();
                    XmlElement root = doc.CreateElement("TheoDoiLCD");
                    doc.AppendChild(root);
                    foreach (DTO.DTOInfo x in lInf) {
                        DTO.TheoDoiLCDInfo it = x as DTO.TheoDoiLCDInfo;
                        if (it == null) return "";

                        XmlElement row = doc.CreateElement("row");

/* Can not combine this                         if (it.<%PrimaryNotAccColumn(Name)%> != null) row.SetAttribute("<%PrimaryNotAccColumn(Name)%>", string.Format("{0}", it.<%PrimaryNotAccColumn(Name)%>));*/
						if (it.XuongId != null) row.SetAttribute("XuongId", string.Format("{0}", it.XuongId));
						if (it.LineId != null) row.SetAttribute("LineId", string.Format("{0}", it.LineId));
						if (it.CongNhan1 != null) row.SetAttribute("CongNhan1", string.Format("{0}", it.CongNhan1));
						if (it.CongNhan2 != null) row.SetAttribute("CongNhan2", string.Format("{0}", it.CongNhan2));
						if (it.CongNhan3 != null) row.SetAttribute("CongNhan3", string.Format("{0}", it.CongNhan3));
						if (it.CongNhan4 != null) row.SetAttribute("CongNhan4", string.Format("{0}", it.CongNhan4));
						if (it.CongNhan5 != null) row.SetAttribute("CongNhan5", string.Format("{0}", it.CongNhan5));
						if (it.CongNhan6 != null) row.SetAttribute("CongNhan6", string.Format("{0}", it.CongNhan6));
						if (it.CongNhan7 != null) row.SetAttribute("CongNhan7", string.Format("{0}", it.CongNhan7));
						if (it.SanLuong1 != null) row.SetAttribute("SanLuong1", string.Format("{0}", it.SanLuong1));
						if (it.SanLuong2 != null) row.SetAttribute("SanLuong2", string.Format("{0}", it.SanLuong2));
						if (it.SanLuong3 != null) row.SetAttribute("SanLuong3", string.Format("{0}", it.SanLuong3));
						if (it.SanLuong4 != null) row.SetAttribute("SanLuong4", string.Format("{0}", it.SanLuong4));
						if (it.SanLuong5 != null) row.SetAttribute("SanLuong5", string.Format("{0}", it.SanLuong5));
						if (it.SanLuong6 != null) row.SetAttribute("SanLuong6", string.Format("{0}", it.SanLuong6));
						if (it.SanLuong7 != null) row.SetAttribute("SanLuong7", string.Format("{0}", it.SanLuong7));
						if (it.SanLuong8 != null) row.SetAttribute("SanLuong8", string.Format("{0}", it.SanLuong8));
						if (it.SanLuong9 != null) row.SetAttribute("SanLuong9", string.Format("{0}", it.SanLuong9));
						if (it.SanLuong10 != null) row.SetAttribute("SanLuong10", string.Format("{0}", it.SanLuong10));
						if (it.SanLuong11 != null) row.SetAttribute("SanLuong11", string.Format("{0}", it.SanLuong11));
						if (it.SanLuong12 != null) row.SetAttribute("SanLuong12", string.Format("{0}", it.SanLuong12));
						if (it.SanLuong13 != null) row.SetAttribute("SanLuong13", string.Format("{0}", it.SanLuong13));
						if (it.SanLuong14 != null) row.SetAttribute("SanLuong14", string.Format("{0}", it.SanLuong14));
						if (it.SanLuong15 != null) row.SetAttribute("SanLuong15", string.Format("{0}", it.SanLuong15));
						if (it.SanLuong16 != null) row.SetAttribute("SanLuong16", string.Format("{0}", it.SanLuong16));
						if (it.SanLuong17 != null) row.SetAttribute("SanLuong17", string.Format("{0}", it.SanLuong17));
						if (it.SanLuong18 != null) row.SetAttribute("SanLuong18", string.Format("{0}", it.SanLuong18));
						if (it.SanLuong19 != null) row.SetAttribute("SanLuong19", string.Format("{0}", it.SanLuong19));
						if (it.SanLuong20 != null) row.SetAttribute("SanLuong20", string.Format("{0}", it.SanLuong20));
						// details
						//if (it.details != null) row.SetAttribute("details", JsonConvert.SerializeObject(it.details));

                        root.AppendChild(row);
                    }

                    result = doc.InnerXml;
                }
            }
            catch (Exception ex) { sErr = ex.Message; }
            return result;
        }

        public override string TransferIn(DTOInfo inf, BUS.BUSControl.TransferMode mode) {
            string sErr = "";
            if (inf is TheoDoiLCDInfo) {
                if (mode == BUS.BUSControl.TransferMode.AddNew) Add((DTO.TheoDoiLCDInfo)inf, ref sErr);
                else if (mode == BUS.BUSControl.TransferMode.Update) sErr = Update((TheoDoiLCDInfo)inf);
                else sErr = InsertUpdate((TheoDoiLCDInfo)inf);
            }
            return sErr;
        }

        public override DTO.DTOInfo[] Preview(XmlDocument xml,ref string sErr, string db = null) {
            DTO.DTOInfo[] lRes = null;
            XmlElement doc = xml.DocumentElement;
            lRes = new DTOInfo[doc.ChildNodes.Count];

            for (int i = 0; i < doc.ChildNodes.Count; i++) {
                if (sErr != "") return lRes;
                try {
                    XmlElement ele = doc.ChildNodes[i] as XmlElement;
               
                    DTO.TheoDoiLCDInfo inf = new DTO.TheoDoiLCDInfo();
					
					//inf.IdSX = db;
/* Can not combine this 					if (GetStringAttribute(ele, "<%PrimaryNotAccColumn(Name)%>") != "") inf.<%PrimaryNotAccColumn(Name)%> = Get<%Type%>Attribute(ele, "<%PrimaryNotAccColumn(Name)%>");*/
                    inf.XuongId = db;
					if (GetStringAttribute(ele, "LineId") != "") inf.LineId = GetInt32Attribute(ele, "LineId");
					if (GetStringAttribute(ele, "CongNhan1") != "") inf.CongNhan1 = GetStringAttribute(ele, "CongNhan1");
					if (GetStringAttribute(ele, "CongNhan2") != "") inf.CongNhan2 = GetStringAttribute(ele, "CongNhan2");
					if (GetStringAttribute(ele, "CongNhan3") != "") inf.CongNhan3 = GetStringAttribute(ele, "CongNhan3");
					if (GetStringAttribute(ele, "CongNhan4") != "") inf.CongNhan4 = GetStringAttribute(ele, "CongNhan4");
					if (GetStringAttribute(ele, "CongNhan5") != "") inf.CongNhan5 = GetStringAttribute(ele, "CongNhan5");
					if (GetStringAttribute(ele, "CongNhan6") != "") inf.CongNhan6 = GetStringAttribute(ele, "CongNhan6");
					if (GetStringAttribute(ele, "CongNhan7") != "") inf.CongNhan7 = GetStringAttribute(ele, "CongNhan7");
					if (GetStringAttribute(ele, "SanLuong1") != "") inf.SanLuong1 = GetInt32Attribute(ele, "SanLuong1");
					if (GetStringAttribute(ele, "SanLuong2") != "") inf.SanLuong2 = GetInt32Attribute(ele, "SanLuong2");
					if (GetStringAttribute(ele, "SanLuong3") != "") inf.SanLuong3 = GetInt32Attribute(ele, "SanLuong3");
					if (GetStringAttribute(ele, "SanLuong4") != "") inf.SanLuong4 = GetInt32Attribute(ele, "SanLuong4");
					if (GetStringAttribute(ele, "SanLuong5") != "") inf.SanLuong5 = GetInt32Attribute(ele, "SanLuong5");
					if (GetStringAttribute(ele, "SanLuong6") != "") inf.SanLuong6 = GetInt32Attribute(ele, "SanLuong6");
					if (GetStringAttribute(ele, "SanLuong7") != "") inf.SanLuong7 = GetInt32Attribute(ele, "SanLuong7");
					if (GetStringAttribute(ele, "SanLuong8") != "") inf.SanLuong8 = GetInt32Attribute(ele, "SanLuong8");
					if (GetStringAttribute(ele, "SanLuong9") != "") inf.SanLuong9 = GetInt32Attribute(ele, "SanLuong9");
					if (GetStringAttribute(ele, "SanLuong10") != "") inf.SanLuong10 = GetInt32Attribute(ele, "SanLuong10");
					if (GetStringAttribute(ele, "SanLuong11") != "") inf.SanLuong11 = GetInt32Attribute(ele, "SanLuong11");
					if (GetStringAttribute(ele, "SanLuong12") != "") inf.SanLuong12 = GetInt32Attribute(ele, "SanLuong12");
					if (GetStringAttribute(ele, "SanLuong13") != "") inf.SanLuong13 = GetInt32Attribute(ele, "SanLuong13");
					if (GetStringAttribute(ele, "SanLuong14") != "") inf.SanLuong14 = GetInt32Attribute(ele, "SanLuong14");
					if (GetStringAttribute(ele, "SanLuong15") != "") inf.SanLuong15 = GetInt32Attribute(ele, "SanLuong15");
					if (GetStringAttribute(ele, "SanLuong16") != "") inf.SanLuong16 = GetInt32Attribute(ele, "SanLuong16");
					if (GetStringAttribute(ele, "SanLuong17") != "") inf.SanLuong17 = GetInt32Attribute(ele, "SanLuong17");
					if (GetStringAttribute(ele, "SanLuong18") != "") inf.SanLuong18 = GetInt32Attribute(ele, "SanLuong18");
					if (GetStringAttribute(ele, "SanLuong19") != "") inf.SanLuong19 = GetInt32Attribute(ele, "SanLuong19");
					if (GetStringAttribute(ele, "SanLuong20") != "") inf.SanLuong20 = GetInt32Attribute(ele, "SanLuong20");
					// details
					//string details = GetStringAttribute(ele, "details");
                    //inf.details = JsonConvert.DeserializeObject<CsDataAccessGroupInfo.RoleDetail[]>(details);
                    lRes[i] = inf;
                }
                catch(Exception ex) { sErr = ex.Message; }
            }
            return lRes;
        }
		public bool validate(JObject o, bool pkOnly, ref string sErr) {
            bool inputErr = false;
			if (pkOnly) {
/* Can not combine this                 if (o["<%PrimaryNotAccColumn(Name)%>"] == null) { sErr += "Invalid <%PrimaryNotAccColumn(Name)%>. Please check again"; inputErr = true; }*/
            } else {
/* Can not combine this 				if (o["<%PrimaryNotAccColumn(Name)%>"] == null) { sErr += "Invalid <%PrimaryNotAccColumn(Name)%>. Please check again"; inputErr = true; }*/
				if (o["XuongId"] != null) { }//Check validate 
				if (o["LineId"] != null) { }//Check validate 
				if (o["CongNhan1"] != null) { }//Check validate 
				if (o["CongNhan2"] != null) { }//Check validate 
				if (o["CongNhan3"] != null) { }//Check validate 
				if (o["CongNhan4"] != null) { }//Check validate 
				if (o["CongNhan5"] != null) { }//Check validate 
				if (o["CongNhan6"] != null) { }//Check validate 
				if (o["CongNhan7"] != null) { }//Check validate 
				if (o["SanLuong1"] != null) { }//Check validate 
				if (o["SanLuong2"] != null) { }//Check validate 
				if (o["SanLuong3"] != null) { }//Check validate 
				if (o["SanLuong4"] != null) { }//Check validate 
				if (o["SanLuong5"] != null) { }//Check validate 
				if (o["SanLuong6"] != null) { }//Check validate 
				if (o["SanLuong7"] != null) { }//Check validate 
				if (o["SanLuong8"] != null) { }//Check validate 
				if (o["SanLuong9"] != null) { }//Check validate 
				if (o["SanLuong10"] != null) { }//Check validate 
				if (o["SanLuong11"] != null) { }//Check validate 
				if (o["SanLuong12"] != null) { }//Check validate 
				if (o["SanLuong13"] != null) { }//Check validate 
				if (o["SanLuong14"] != null) { }//Check validate 
				if (o["SanLuong15"] != null) { }//Check validate 
				if (o["SanLuong16"] != null) { }//Check validate 
				if (o["SanLuong17"] != null) { }//Check validate 
				if (o["SanLuong18"] != null) { }//Check validate 
				if (o["SanLuong19"] != null) { }//Check validate 
				if (o["SanLuong20"] != null) { }//Check validate 
			}
			return inputErr;
		}
		public override bool isField(string field) {
            return new DTO.TheoDoiLCDInfo().isField(field);
        }
		#endregion Method

    }
}
